﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Data.Linq.SqlClient;

namespace RouterControl.Db
{
	public static class DatabaseProperties
	{
		private static readonly ILog _log = LogManager.GetLogger(typeof(DatabaseProperties));

		public static DateTime LastDataWarehouseUpdate
		{
			get
			{
				try
				{
					using (var ctx = DeviceDomDbConnector.GetContext())
					{
						var propVal = (
							from p in ctx.PropertyDbs
							where SqlMethods.Like(p.PropName, "LastDataWarehouseUpdate")
							select p.PropValue).Take(1).FirstOrDefault();

						if (string.IsNullOrEmpty(propVal))
							return DateTime.MinValue;

						return DateTime.Parse(propVal);
					}
				}
				catch (Exception e)
				{
					_log.Error("Failed to retrieve property: LastDataWarehouseUpdate", e);

					throw;
				}
			}
		}
	}
}
